package com.waves.task.event.listener;

import cn.hutool.core.util.BooleanUtil;
import com.waves.core.constant.redis.TaskRedisKey;
import com.waves.core.utils.RedisUtils;
import com.waves.task.domain.pojo.TaskRedisSetPOJO;
import com.waves.task.event.TaskRedisSetEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 任务点赞、取消赞、收藏、取消收藏等操作的事件监听
 */
@Component
public class TaskRedisSetListener {

    @Order(1)
    @Async
    @EventListener
    public void handleEvent(TaskRedisSetEvent event) {
        TaskRedisSetPOJO pojo = event.getPojo();
        String key = TaskRedisKey.getKey(pojo.getRedisSetTypeEnum().getKey(), pojo.getKey());
        Boolean member = RedisUtils.sIsMember(key, pojo.getMember());
        if (BooleanUtil.isTrue(member)) {
            RedisUtils.sRemove(key, pojo.getMember());
        } else {
            RedisUtils.sSetAndTime(key, pojo.getTimes(), pojo.getMember());
        }
    }


}
